package com.ytt.core.service.entity.user;

import com.ytt.core.service.entity.AbstractBaseEntity;
import com.ytt.core.service.entity.college.College;
import com.ytt.core.service.entity.role.Role;
import com.ytt.core.service.entity.schoolClass.SchoolClass;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * User
 *
 * @author chenwen
 * @date 2019/9/12 15:01
 */
@Entity
@Table(name = "sys_user")
@Setter
@Getter
public class User extends AbstractBaseEntity {

    /**
     * 姓名
     */
    @Column(name = "name",length = 60)
    private String name;
    
    /**
     * 登录账号, 输入时，要求检查唯一性
     */
    @Column(name = "account", length = 20, unique = true, nullable = false)
    private String account;

    /**
     * 密码
     */
    @Column(name = "password", length = 60)
    private String password;

    /**
     * 密码失效时间，如果为null，则表示永不失效
     * 只有localAccount为true才有效
     */
    @Column(name = "password_expire_date")
    private Date passwordExpireDate;

    /**
     * 账号是否已激活, 用于临时锁定账号，主要用于特殊验证（例如，邮箱激活）
     * 只有localAccount为true才有效
     */
    @Column(name = "is_enabled", nullable = false)
    private Boolean enabled = true;

    /**
     * 账号的有效期，如果为null，则表示永久有效，主要用于设置系统临时访问账号
     * 只有localAccount为true才有效
     */
    @Column(name = "account_expire_date")
    private Date accountExpireDate;


    /**
     * 是否本地登录账号, 如果是则使用password来验证密码， 如不是，则使用其他方式
     */
    @Column(name = "is_local_account", nullable = false)
    private Boolean localAccount = true;

    /**
     * 邮箱，用于找回密码
     */
    @Column(name = "mail", nullable = false)
    private String mail ;


    /**
     * 所属的角色
     */
    @ManyToMany
    @JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    @OrderBy("name asc")
    private Set<Role> roles = new LinkedHashSet<>();

    /**
     * 所属的班级
     */
    @ManyToOne(targetEntity = SchoolClass.class)
    @JoinColumn(name = "schoolclass_id")
    private SchoolClass schoolClass;

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
